﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Fadada.FASC.SDK.DotNetCore.Utils
{
    public class SignUtil
    {
        /// <summary>
        ///  生成签名
        /// </summary>
        /// <param name="input"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static string GetSign(Dictionary<string, string> dic, string timestamp, string appkey)
        {
            string sign = null;
            //dic = dic.Where(r => string.IsNullOrEmpty(r.Value) == false).OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);
            //var content = string.Join("&", dic.Select(r => r.Key + "=" + r.Value));
           // var dicSort = from objDic in dic orderby(objDic.Key, StringComparer.Ordinal) select objDic; //升序排列
           var dicSort = dic.OrderBy(x => x.Key, StringComparer.Ordinal).ToDictionary(x => x.Key, x => x.Value);
           var content = string.Join("&", dicSort.Select(kvp => $"{kvp.Key}={kvp.Value}"));
            string signText = CryptTool.sha256(content).ToLower();
            byte[] secretSign = CryptTool.HMACSHA256Byte(timestamp, appkey);
            sign = CryptTool.HMACSHA256Str(signText.ToLower(), secretSign);
            return sign;
        }
    }
}